<template>
  <div>
    <el-dialog
      width="1024px"
      :close-on-click-modal="false"
      :show-close="true"
      v-bind="$attrs"
      v-on="$listeners"
      @open="onOpen"
      @close="onClose"
    >
      <div slot="title">
        {{ formData.id != null ? '修改联系人' : '新增联系人' }}
      </div>
      <div class="sketch_content">
        <el-form label-position="top" ref="elForm" :model="formData" :rules="rules" label-width="80px">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="姓名" prop="name">
                <el-input v-model="formData.name" placeholder="请输入" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="客户名称" prop="customerName">
                <el-input @click.native="customerIdClick" placeholder="请选择" v-model="formData.customerName"
                          style="width: 100%"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input v-model="formData.mobile" prefix-icon="el-icon-mobile-phone" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="电话" prop="telephone">
                <el-input v-model="formData.telephone" prefix-icon="el-icon-phone-outline" style="width: 100%"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="职务" prop="post">
                <el-input v-model="formData.post" prefix-icon="el-icon-message" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="邮箱" prop="email">
                <el-input v-model="formData.email" prefix-icon="el-icon-discover" style="width: 100%"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="是否关键决策人" prop="decisionStatus">
                <el-select v-model="formData.decisionStatus" placeholder="请选择" style="width: 100%">
                  <el-option
                    v-for="dict in dict.type.sys_crm_decision"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="性别" prop="sexId">
                <el-select v-model="formData.sexId" placeholder="请选择" style="width: 100%">
                  <el-option
                    v-for="dict in dict.type.sys_user_sex"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="地址" prop="address">
                <el-input v-model="formData.address" prefix-icon="el-icon-discover" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="下次联系时间" prop="nextTime">
                <el-date-picker value-format="yyyy-MM-dd hh:mm:ss" v-model="formData.nextTime" type="datetime"
                                placeholder="请选择" style="width: 100%"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input
                  type="textarea"
                  :autosize="{ minRows: 3, maxRows: 5}"
                  placeholder="请输入内容"
                  v-model="formData.remark"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="submitForm" :loading="submitLoading">保 存</el-button>
        <el-button size="small" @click="onClose">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 客户选择 -->
    <customer-select-popover ref="customerSelectPopover" @callback="customerDataCallback"></customer-select-popover>
  </div>
</template>

<script>
import {
  saveHasKey,
  selectByPrimaryKey,
  updateByPrimaryKeySelective
} from '@/api/crm/Contacts'

import CustomerSelectPopover from '@/components/Crm/Customer/CustomerSelectPopover'

export default {
  inheritAttrs: false,
  name: 'ContactsDialog',
  dicts: ['sys_user_sex', 'sys_crm_decision'],
  components: { CustomerSelectPopover },
  props: {
    pk: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      title: '添加联系人', // 对话框标题
      dialogVisible: false, // 对话框懒加载
      submitLoading: false, // 提交按钮加载动画
      formData: {
        id: null, // 主键id
        name: '', // 联系人姓名
        customerName: '', // 客户姓名
        customerId: '', // 客户id
        mobile: '', // 手机
        telephone: '', // 电话
        website: '', // 网址
        email: '', // 邮箱
        post: '', // 职务
        address: '', // 地址
        nextTime: '', // 下次联系时间
        sexId: null, // 性别id
        decisionStatus: null, // 是否关键决策人
        remark: '' // 备注
      },
      // 规则
      rules: {
        customerName: [{ required: true, message: '客户名称不能为空', trigger: 'change' }],
        name: [{ required: true, message: '联系人姓名不能为空', trigger: 'blur' }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }],
        telephone: [{ required: true, message: '电话不能为空', trigger: 'blur' }]
      }
    }
  },
  // 在实例创建完成后被立即调用
  created() {
  },
  // 实例被挂在后调用
  mounted() {

  },
  // 方法
  methods: {
    // 对话框打开
    onOpen() {
      if (this.pk != null) {
        selectByPrimaryKey(this.pk).then(res => {
          this.formData = res.data
        })
      }
    },
    // 对话框关闭回调
    onClose() {
      this.submitLoading = false
      this.$refs['elForm'].resetFields()
      this.$emit('update:visible', false)
    },
    // 提交
    submitForm() {
      this.submitLoading = true
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          if (this.formData.id != undefined) {
            // 修改
            updateByPrimaryKeySelective(this.formData).then(res => {
              this.$modal.msgSuccess('修改成功')
              this.$emit('ok')
              this.onClose()
            })
          } else {
            // 新增
            saveHasKey(this.formData).then(res => {
              this.$modal.msgSuccess('新增成功')
              this.$emit('ok')
              this.onClose()
            })
          }
        }
      })
    },
    // 客户Input点击
    customerIdClick() {
      this.$refs.customerSelectPopover.open()
    },
    // 客户选择完成回调函数
    customerDataCallback(data) {
      this.formData.customerId = data.id
      this.formData.customerName = data.customerName
    }
  }
}
</script>

<style scoped>
</style>
